package com.huan.dubbo.service.spi;

import com.huan.dubbo.service.OrderPay;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Adaptive;
import org.apache.dubbo.common.extension.ExtensionAccessor;
import org.apache.dubbo.common.extension.ExtensionAccessorAware;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.model.ApplicationModel;

/**
 * OrderPay 接口 适应类
 *
 *  问题：看底层源代码一个扩展类是存在多个适应类的。但是如果存在多个适应类，那么就会覆盖前面的适应类。也就是说只有一个适应类生效！
 *         还要注意的是。多个适应类的是否要覆盖是根据你加载的策略对象想关联的！看了dubbo的四种加载策略发现都是默认覆盖了
 *   遍历加载策略，加载策略有4种：
 *              1. DubboInternalLoadingStrategy  加载  META-INF/dubbo/internal/ 下的文件
 *              2. DubboExternalLoadingStrategy  加载  META-INF/dubbo/external/ 下的文件
 *              3. DubboLoadingStrategy  加载  META-INF/dubbo/ 下的文件
 *              4. ServicesLoadingStrategy  加载  META-INF/services/ 下的文件
 */
@Adaptive
public class AdaptivePayOrder implements OrderPay, ExtensionAccessorAware {

    private ExtensionAccessor extensionAccessor;
    private String orderName;

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    @Override
    public String pay() {
        OrderPay orderPay;
        if (StringUtils.isEmpty(orderName)) {
            orderPay = extensionAccessor.getDefaultExtension(OrderPay.class);
        } else {
            orderPay = extensionAccessor.getExtension(OrderPay.class, orderName);
        }
        return orderPay.pay();
    }

    @Override
    public String payAdaptive(URL url) {
        System.out.println("@Adaptive 类 ");
        return "Adaptive class";
    }

    @Override
    public void setExtensionAccessor(ExtensionAccessor extensionAccessor) {
        this.extensionAccessor = extensionAccessor;
    }
}
